package com.example.gobang.api;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * Created with IntelliJ IDEA.
 * ClassName: TestAPI
 * Package: com.example.gobang.api
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-02-23
 * Time: 21:11
 */


public class TestAPI extends TextWebSocketHandler {

    //目前只有这个类还不够 要注册到Spring框架当中去
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("连接成功！");
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("收到消息: " + message.getPayload());
        //让服务器收到消息之后返回给客户端
        //web session 和http的会话不一样
        session.sendMessage(message);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("异常连接");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("连接关闭");
    }
}
